package com.mikufufu.blog.service.impl;

import com.mikufufu.blog.service.SysSettingService;
import com.mikufufu.blog.storage.enums.StorageType;
import com.mikufufu.blog.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mikufufu.blog.mapper.StorageMapper;
import com.mikufufu.blog.model.entity.Storage;
import com.mikufufu.blog.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 存储方式 服务实现类
 * 
 */
@Slf4j
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements StorageService{
    @Autowired
    private SysSettingService sysSettingService;

    @Override
    public String getStorageType(String storageCode) {
        Integer type = getStorage(storageCode).getStorageType();
        StorageType storageType = StorageType.getStorageType(type);
        return storageType!=null?storageType.getCode():null;
    }

    @Override
    public Storage getStorage(String storageCode) {
        if(StringUtils.isBlank(storageCode)){
            throw new RuntimeException("存储服务商编码为空");
        }
        return this.getOne(new LambdaQueryWrapper<Storage>()
                .eq(Storage::getStorageCode,storageCode)
        );
    }

    @Override
    public Storage getStorage() {
        return getStorage(sysSettingService.getStorageCode());
    }

    @Override
    public Boolean updateStorage(Storage storage) {
        return this.update(storage,new LambdaUpdateWrapper<Storage>()
                .eq(Storage::getStorageCode,storage.getStorageCode()));
    }

    @Override
    public Boolean deleteStorage(String storageCode) {
        return this.remove(new LambdaQueryWrapper<Storage>()
                .eq(Storage::getStorageCode,storageCode));
    }

}
